<?php

class Empeval_Methods_Request_AddBonusGoalsForm {
	
	const EMPTY_ROW_COUNT = 1;
	
	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	public $userId;
	
	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
		$this->userId = ($params->userId != "") ? $params->userId : null;
	}
	
	public function process() {
		$user = $this->getUserWithPosition();
		if (!($user instanceof EmpevalUsers))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);
		$formType = $this->getFormType($user);
		if ($formType === null)
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_USER_DO_NOT_HAVE_ASSIGNED_GRADE, array($user->getId()));
		$dateNow = new DateTime('now');
		
		$con = Propel::getConnection('vgp_empeval');
		$con->beginTransaction();
		try {
			$bonusGoalsForm = new EmpevalBonusGoalsForms();
			$bonusGoalsForm
				->setUserPositionId($user->getUserPositionss()->getFirst()->getId())
				->setYear($dateNow->format('Y'))
				->setType($formType)
				->setStatus(EmpevalBonusGoalsForms::STATUS_NEW)
				->setActive(1)
				->save($con);
			
			if ($formType == EmpevalBonusGoalsForms::LOW_GRADE_FORM) {
				$indicator = EmpevalIndicatorsQuery::create()->findOne();
				for ($i = 0; $i < self::EMPTY_ROW_COUNT; $i++) {
					$kpi = new EmpevalKpi();
					$kpi->setIndicators($indicator);
					$kpi->setTrend('higher');
					$kpi->setWeight(null);
					$kpi->setStatus(EmpevalKpi::STATUS_ACTIVE);
					$kpi->setBonusGoalsFormId($bonusGoalsForm->getId());
					$kpi->save($con);
					
// 					$taskGoal = new EmpevalTaskGoals();
// 					$taskGoal->setWeight(null);
// 					$taskGoal->setStatus(EmpevalTaskGoals::STATUS_ACTIVE);
// 					$taskGoal->setBonusGoalsFormId($bonusGoalsForm->getId());
// 					$taskGoal->save($con);
				}
			}
			else {
				for ($i = 0; $i < self::EMPTY_ROW_COUNT; $i++) {
					$mbo = new EmpevalMboGoals();
					$mbo->setWeightingFactor(null);
					$mbo->setDegreeOfFulfillment(0);
					$mbo->setType(EmpevalMboGoals::TYPE_FUNCTION);
					$mbo->setStatus(EmpevalMboGoals::STATUS_ACTIVE);
					$mbo->setBonusGoalsFormId($bonusGoalsForm->getId());
					$mbo->save($con);
					
					$mbo = new EmpevalMboGoals();
					$mbo->setWeightingFactor(null);
					$mbo->setDegreeOfFulfillment(0);
					$mbo->setType(EmpevalMboGoals::TYPE_PERSONAL);
					$mbo->setStatus(EmpevalMboGoals::STATUS_ACTIVE);
					$mbo->setBonusGoalsFormId($bonusGoalsForm->getId());
					$mbo->save($con);
				}
			}
			$con->commit();
		} catch (Exceptipon $e) {
			$con->rollBack();
			throw $e;
		}
		
		return new Empeval_Methods_Response_AddBonusGoalsForm(true, $bonusGoalsForm->getId());
	}
	
	private function getFormType(EmpevalUsers $user) {
		$userGrade = $user->getUserPositionss()->getFirst()->getPositions()->getGrade();
		if ($userGrade == null)
			return null;
		$toGrade = (int)Empeval_Config::getInstance()->bonusGoalsForm->firstFormToGrade;
		return ($userGrade <= $toGrade) ? EmpevalBonusGoalsForms::LOW_GRADE_FORM : EmpevalBonusGoalsForms::HIGH_GRADE_FORM;
	}
	
	private function getUserWithPosition() {
			$userId = ($this->userId === null) ? Empeval_Auth::getInstance()->getUser()->getId() : $this->userId;
			$user = EmpevalUsersQuery::create()
				->filterById($userId)
				->useUserPositionsQuery()
					->filterByActive(1)
					->joinPositions()
				->endUse()
				->with('UserPositions')
				->with('Positions')
				->findOne();
			return $user;
	}
	
}